package mtc.mtc;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.text.format.Formatter;
import android.util.Xml;

public class Util {

    public static Boolean DEV_MODE = false;
    static ConnectivityManager network;

    public static boolean isUsingWiFi(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifiInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (wifiInfo != null) {
            if (wifiInfo.getState() == NetworkInfo.State.CONNECTED || wifiInfo.getState() == NetworkInfo.State.CONNECTING) {
                return true;
            }
        }

        return false;
    }

    public static boolean isUsingMobileData(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo mobileInfo = connectivity
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (mobileInfo != null
                && mobileInfo.getState() == NetworkInfo.State.CONNECTED
                || mobileInfo.getState() == NetworkInfo.State.CONNECTING) {
            return true;
        }

        return false;
    }

    @SuppressWarnings("deprecation")
    public static List<String> getWifiInfo(Context context) {
        WifiManager wifi = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcp = wifi.getDhcpInfo();

        // Whatever string is given is what outputs
        List<String> info = new ArrayList<String>();
        //ipaddr 10.0.3.15 gateway 10.0.3.2 netmask 255.255.255.0 dns1 172.23.232.8 dns2 172.23.232.20 DHCP server 10.0.3.2 lease 86400 seconds
        if (DEV_MODE) {
            info.add("192.168.1.1");
        } else {
            info.add(Formatter.formatIpAddress(dhcp.gateway));
            info.add(Formatter.formatIpAddress(dhcp.ipAddress));
        }
        // The following come from Wifi Info
        /*
         * WifiInfo wifiInfo = wifi.getConnectionInfo(); info.add("SSID: " +
         * wifiInfo.getSSID()); info.add("BSSID: " + wifiInfo.getBSSID());
         * info.add("Hidden: " + wifiInfo.getHiddenSSID()); info.add("MAC: " +
         * wifiInfo.getMacAddress()); info.add("Speed: " +
         * WifiInfo.LINK_SPEED_UNITS);
         *
         * // The following come from DHCP Info info.add("IP: " +
         * Formatter.formatIpAddress(dhcp.ipAddress)); info.add("DNS 1: " +
         * Formatter.formatIpAddress(dhcp.dns1)); info.add("DNS 2: " +
         * Formatter.formatIpAddress(dhcp.dns2)); info.add("DHCP: " +
         * Formatter.formatIpAddress(dhcp.serverAddress)); info.add("Subnet: " +
         * Formatter.formatIpAddress(dhcp.netmask)); info.add("Lease Duration: "
         * + dhcp.leaseDuration);
         */

        return info;
    }

};